package com.newshub.admin.service.impl;

import com.newshub.admin.service.DashboardService;
import com.newshub.business.service.NewsService;
import com.newshub.business.service.SourceService;
import com.newshub.business.service.CategoryService;
import com.newshub.business.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 仪表盘服务实现类
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private NewsService newsService;

    @Autowired
    private SourceService sourceService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UsersService usersService;

    @Override
    public Map<String, Object> getDashboardOverview() {
        Map<String, Object> overview = new HashMap<>();
        
        try {
            // 获取新闻趋势数据
            overview.put("newsTrend", getNewsTrend("day"));
            
            // 获取信源分布数据
            overview.put("sourceDistribution", getSourceDistribution());
            
            // 获取分类统计数据
            overview.put("categoryStats", getCategoryStats());
            
            // 获取用户统计数据
            overview.put("userStats", usersService.getUserStats());
            
        } catch (Exception e) {
            // 如果某个统计失败，记录错误但不影响其他数据
            overview.put("error", "部分数据获取失败: " + e.getMessage());
        }
        
        return overview;
    }

    @Override
    public Map<String, Object> getNewsTrend(String period) {
        Map<String, Object> trendData = new HashMap<>();
        
        try {
            // 根据周期获取新闻趋势数据
            Map<String, Object> trendStats = newsService.getNewsTrendStats(period);
            trendData.put("period", period);
            trendData.put("data", trendStats);
            
        } catch (Exception e) {
            trendData.put("error", "获取新闻趋势数据失败: " + e.getMessage());
        }
        
        return trendData;
    }

    @Override
    public Map<String, Object> getSourceDistribution() {
        Map<String, Object> distributionData = new HashMap<>();
        
        try {
            // 获取信源分布数据
            Map<String, Object> sourceStats = sourceService.getSourceDistributionStats();
            distributionData.put("data", sourceStats);
            
        } catch (Exception e) {
            distributionData.put("error", "获取信源分布数据失败: " + e.getMessage());
        }
        
        return distributionData;
    }

    @Override
    public Map<String, Object> getCategoryStats() {
        Map<String, Object> categoryData = new HashMap<>();
        
        try {
            // 获取分类统计数据
            Map<String, Object> categoryStats = categoryService.getCategoryStats();
            categoryData.put("data", categoryStats);
            
        } catch (Exception e) {
            categoryData.put("error", "获取分类统计数据失败: " + e.getMessage());
        }
        
        return categoryData;
    }
}

